package com.business_clean.ui.fragment.addproject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.Observer;

import com.business_clean.app.App;
import com.business_clean.app.base.BaseFragment;
import com.business_clean.app.config.ConstantMMVK;
import com.business_clean.app.uitl.MMKVHelper;
import com.business_clean.app.uitl.ToastUtil;
import com.business_clean.data.mode.members.IDCardMemberEntity;
import com.business_clean.data.mode.members.MembersDetailEntity;
import com.business_clean.databinding.FragmentBankCardBinding;
import com.business_clean.databinding.FragmentSalaryBinding;
import com.business_clean.ui.activity.custom.AddProjectActivity;
import com.business_clean.viewmodel.request.ProjectMembersViewModel;
import com.business_clean.viewmodel.request.ProjectPersonnelViewModel;

import org.jetbrains.annotations.Nullable;

import java.util.HashMap;

import me.hgj.mvvmhelper.base.BaseViewModel;

public class SalaryFragment extends BaseFragment<ProjectMembersViewModel, FragmentSalaryBinding> {

    private String uuid = "";

    private int type = 0;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        getMDatabind().withSalary.setInputNumber();
    }


    @Override
    public void onBindViewClick() {
        getMDatabind().bottom.butBottomButLast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.appViewModelInstance.getProjectLastPersonnel().setValue(3);
            }
        });
        getMDatabind().bottom.butBottomButNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(getMDatabind().withSalary.getInputText())) {
                    ToastUtil.show("请输入薪酬标准");
                    return;
                }
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("op_type", "salary");
                hashMap.put("uuid", uuid);
                hashMap.put("salary", "" + getMDatabind().withSalary.getInputText());
                mViewModel.requestUpdateMembers(hashMap, AddProjectActivity.ID_SALARY, type);
            }
        });
    }


    @Override
    public void initObserver() {
        //新建的uuid
        App.appViewModelInstance.getCreateUuid().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                uuid = s;
            }
        });
    }

    @Override
    public void lazyLoadData() {
        if (getArguments() != null) {//说明已经存在了
            uuid = getArguments().getString("uuid");
            type = getArguments().getInt("type");

            String application_no = getArguments().getString("application_no");


            HashMap<String, String> hashMap = new HashMap<>();
            if (!TextUtils.isEmpty(uuid)) {
                hashMap.put("uuid", uuid);
            }
            if (!TextUtils.isEmpty(application_no)) {
                hashMap.put("application_no", application_no);
            }
            //1是草稿编辑  2是草稿新建 3是档案详情
            if (type == 3) {
                mViewModel.requestStaffDetail(hashMap);//真正的详情
            } else {
                mViewModel.requestMembersDetail(hashMap);//草稿的详情，还没真的成为员工
                getMDatabind().withSalary.setInputText(MMKVHelper.getString(ConstantMMVK.WORK_JOB_SALARY));//取本地默认的值
            }
        } else {
            getMDatabind().withSalary.setInputText(MMKVHelper.getString(ConstantMMVK.WORK_JOB_SALARY));//取本地默认的值
        }
    }


    @Override
    public void onRequestSuccess() {
        mViewModel.getUpdateSalaryStatus().observe(this, new Observer<IDCardMemberEntity>() {
            @Override
            public void onChanged(IDCardMemberEntity idCardMemberEntity) {
                App.appViewModelInstance.getProjectPersonnel().setValue(5);
            }
        });

        mViewModel.getMembersDetailEntity().observe(this, new Observer<MembersDetailEntity>() {
            @Override
            public void onChanged(MembersDetailEntity membersDetailEntity) {
                uuid = membersDetailEntity.getUuid();
                if (!TextUtils.isEmpty(membersDetailEntity.getSalary())) {
                    getMDatabind().withSalary.setInputText(membersDetailEntity.getSalary());
                }
            }
        });
    }
}
